package com.capinfo.accounting.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.NativeWebRequest;

import com.capinfo.accumulation.model.accounting.mybist.GB_JZPZXX;
import com.capinfo.accumulation.model.pagemodel.Json;
import com.capinfo.accumulation.parameter.accounting.DataGrid;
import com.capinfo.accumulation.service.accounting.mybits.GB_JZPZXXServiceI;
import com.capinfo.accumulation.service.accounting.mybits.HZJZPZServiceI;
import com.capinfo.commons.project.model.security.SystemUser;
import com.capinfo.components.security.web.utils.WebRequestUtils;
import com.google.gson.Gson;




@Controller
@RequestMapping("/business/accounting/pingzhengjizhang")
public class PZJZController {
	@Autowired
	public GB_JZPZXXServiceI jzpxxService;
	@Autowired
	HZJZPZServiceI hZJZPZService;
	//应该是查询吧
	
	@RequestMapping("/dataGrid.shtml")
	@ResponseBody
	public DataGrid dataGrid(@RequestParam  Map param) {
		return jzpxxService.findByFH(param);
	} 
	//记账
	@RequestMapping("/update.shtml")
	@ResponseBody
	public Json update(String ids, HttpSession session,NativeWebRequest request){
		GB_JZPZXX jzpzxx =new GB_JZPZXX();
		Json j = new Json();
		long id=0;
		SystemUser user = (SystemUser)WebRequestUtils.retrieveSecureUser(request);
		String jz=user.getLogonName();
		
	    Date date=new Date();
		String[] idsArray=ids.split(",");
	
		try {
			
			for(int i=0;i<idsArray.length;i++){
				 id=Long.parseLong(idsArray[i]);
				 	jzpzxx.setID(id);
					jzpzxx.setJZ(jz);
					jzpzxx.setJZRQ(date);
					jzpxxService.updateJZByID(jzpzxx);
			}
			
			j.setSuccess(true);
			j.setMsg("记账成功");
		} catch (Exception e) {
			e.printStackTrace();
			j.setMsg(e.getMessage());
			
		}
		return j;
		
	}
	//取消记账
		@RequestMapping("/removeUpdate.shtml")
		@ResponseBody
		public Json removeUpdate(String ids, HttpSession session,NativeWebRequest request){
			GB_JZPZXX jzpzxx =new GB_JZPZXX();
			Json j = new Json();
			long id=0;
			String jz="";
		    Date date=null;
			String[] idsArray=ids.split(",");
		
			try {
				
				for(int i=0;i<idsArray.length;i++){
					 id=Long.parseLong(idsArray[i]);
					 	jzpzxx.setID(id);
						jzpzxx.setJZ(jz);
						jzpzxx.setJZRQ(date);
						jzpxxService.updateJZByID(jzpzxx);
				}
				
				j.setSuccess(true);
				j.setMsg("取消成功");
			} catch (Exception e) {
				e.printStackTrace();
				j.setMsg(e.getMessage());
				
			}
			return j;
			
		}
		@RequestMapping(value="/hsdw")  
		@ResponseBody  
		public void hsdw (HttpServletRequest request,HttpServletResponse response) throws IOException  {
			response.setHeader("Content-type", "text/html;charset=UTF-8");
			response.setCharacterEncoding("UTF-8");
			 Gson gson = new Gson();
			   Map map = new HashMap();
			   List<Map<String,Object>>hsdw= hZJZPZService.hsdw();
			   PrintWriter pw;
			   pw = response.getWriter();
			   pw.write(gson.toJson(hsdw));
		   }
		

		
		
		@RequestMapping(value="/cwzt")  
		@ResponseBody  
		public void cwzt (HttpServletRequest request,HttpServletResponse response) throws IOException  {
			response.setHeader("Content-type", "text/html;charset=UTF-8");
			response.setCharacterEncoding("UTF-8");
			 Gson gson = new Gson();
			   Map map = new HashMap();
			   List<Map<String,Object>>cwzt= hZJZPZService.cwzt();
			   PrintWriter pw;
			   pw = response.getWriter();
			   pw.write(gson.toJson(cwzt));
		   }

	

}
